<?php

/**
 * This is the model class for table "{{mem_account}}".
 *
 * The followings are the available columns in table '{{mem_account}}':
 * @property integer $zmc_id
 * @property integer $zm_id
 * @property integer $zad_id
 * @property integer $zmc_type
 * @property integer $zmc_event
 * @property string $zmc_orderid
 * @property double $zmc_amount
 * @property double $zmc_amount_log
 * @property string $zmc_note
 * @property integer $inputtime
 */
class MemAccount extends CActiveRecord
{
	//商家名称，用于筛选
	public $zc_name;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return MemAccount the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{mem_account}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zm_id, zad_id, zmc_type, zmc_event, inputtime', 'numerical', 'integerOnly'=>true),
			array('zmc_amount, zmc_amount_log', 'numerical'),
			array('zmc_orderid', 'length', 'max'=>30),
			array('zmc_note,zc_name', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zmc_id, zm_id, zad_id, zmc_type, zmc_event, zmc_orderid, zmc_amount, zmc_amount_log, zmc_note, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				 'company'=>array(self::BELONGS_TO, 'company', 'zm_id'),//商家
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zmc_id' => 'ID',
			'zm_id' => '会员ID',
			'zad_id' => '管理员ID',
			'zmc_type' => '类型',
			'zmc_event' => '操作类型',
			'zmc_orderid' => '订单ID/流水号',
			'zmc_amount' => '金额',
			'zmc_amount_log' => '余额',
			'zmc_note' => '备注',
			'inputtime' => '创建时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zmc_id',$this->zmc_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zad_id',$this->zad_id);
		$criteria->compare('zmc_type',$this->zmc_type);
		$criteria->compare('zmc_event',$this->zmc_event);
		$criteria->compare('zmc_orderid',$this->zmc_orderid,true);
		$criteria->compare('zmc_amount',$this->zmc_amount);
		$criteria->compare('zmc_amount_log',$this->zmc_amount_log);
		$criteria->compare('zmc_note',$this->zmc_note,true);
		$criteria->compare('inputtime',$this->inputtime);
		
		if(!empty($this->zc_name)){
			$criteria->join = ' LEFT JOIN {{company}} com ON com.zm_id = t.zm_id ';
			$criteria->addCondition(' com.zc_name like "%'.$this->zc_name.'%" ');
		}

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}		
	
	/**
	 * 操作类型:
	 */
	public function getZmcType($type=null){
		if($type==null){
			return array(
					''=>'请选择',
					'1'=>'收入',
					'2'=>'支出',
					'3'=>'套现'
			);
		}else{
			$level = $this->getZmcType();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	 /**
	 *判断操作类型是1增还是2减
	 */
	 protected function beforeSave(){
	    if (parent::beforeSave()){   
	    	//会员余额功能未启用，此表只做为流水怅记录表，不涉及余额变动
		 //   $member= Member::model()->findByAttributes(array('zm_id'=>$this->zm_id));
		 //   $zmc_amount = $member->attributes['zm_balance'];
	     /*   if($this->zmc_type=='1'){
				//当前会员余额增加
	           $this->zmc_amount_log = $zmc_amount + $this->zmc_amount;
	        }else if($this->zmc_type=='2'){
				//当前会员余额减少
			   $this->zmc_amount_log = $zmc_amount - $this->zmc_amount;
			}   	*/		
		    $this->inputtime = time();	 
	        return true;
	    }else{
	        return false;
	    }
	}
}